
//
//  FirstViewController.m
//  BLDMusics
//
//  Created by 白云杰 on 16/3/26.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@property (strong, readwrite, nonatomic) UITableView *tableView;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIImageView *imgView;
@property (nonatomic, retain) NSDictionary *dic;

@end

@implementation FirstViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dic = [NSDictionary dictionary];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [_tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 54 * 5) / 2.0f, self.view.frame.size.width, 54 * 5) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.opaque = NO;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView = nil;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.bounces = NO;
        tableView;
    });
    [self.view addSubview:self.tableView];

    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 2, 50, 50)];
    _imgView.backgroundColor = [UIColor clearColor];
    _imgView.layer.cornerRadius = 25;
    _imgView.layer.masksToBounds = YES;
    _imgView.image = [UIImage imageNamed:@"myfavorite.jpg"];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, SCREEN_WIDTH / 2, 50)];
    _label.font = [UIFont systemFontOfSize:20];
    _label.textColor = [UIColor whiteColor];
//    _label.text = @"登陆/注册";
    [self createKVO];
}


- (void)createKVO {
    
    // 获取到单例对象
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(observerAction:) name:@"Landing" object:nil];
}

#pragma mark UITableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 1:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 0:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[BYJLandingViewController alloc] init]]
                                                         animated:YES];
    
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 2:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[BYJSleepSetViewController alloc] init]]
                                                         animated:YES];
           
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 3:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[BYJSetViewController alloc] init]]
                                                         animated:YES];
            
            [self.sideMenuViewController hideMenuViewController];
            break;
            case 4:
            [BmobUser logout];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"microblog"];
            [_tableView reloadData];
            break;
        default:
            break;
    }
    
    
}

#pragma mark UITableView Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc] init];
    }
    BmobUser *bUser = [BmobUser getCurrentUser];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *arr = [userDefaults arrayForKey:@"microblog"];
    
    if (indexPath.row == 0) {
        
        [cell addSubview:_imgView];
        
        
        if (bUser && arr.count == 0) {
            _label.text = bUser.username;
        }else if (arr) {
            _label.text = arr[0];
            [_imgView sd_setImageWithURL:[NSURL URLWithString:arr[1]]];
        } else {
            _label.text = @"登陆/注册";
            _imgView.image = [UIImage imageNamed:@"myfavorite.jpg"];
        }
        
        
        [cell addSubview:_label];
    } else {
        cell.imageView.image = [UIImage imageNamed:@"cell_icon_uncollected~iphone.png"];
        NSArray *titles = @[@"首页", @"睡眠设置", @"设置", @"退出登陆"];
        
        cell.textLabel.text = titles[indexPath.row - 1];
    }
 
    return cell;
}

- (void)observerAction:(NSNotification *)notification {

//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//    UITableViewCell *cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
    
//    self.dic = notification.object;
//    _label.text = _dic[@"name"];
//    [_imgView sd_setImageWithURL:[NSURL URLWithString:_dic[@"icon"]]];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
